<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class Menu extends CI_Controller {
		
		protected $action="";
		
		function __construct(){
			parent::__construct();
			$this->load->model("Menu_model");
			$this->load->helper('url');
		}
		/* 
			Library nextid()
		*/
		function nextid($tablename,$idfield){
			$sqlspec="SELECT MAX($idfield) AS latestId FROM $tablename";
			
			$query = $this->db->query("SELECT MAX($idfield) AS latestId FROM $tablename");
			$row = $query->row_array();
			$latestId=$row['latestId'];
	 
			$nowId=1;
			if (!empty($latestId)){
				$nowId=$latestId+1;
			}
			return $nowId;
		}
 
		function sort_item(){
			/* 
				@data $data= array dari field sort di form
			*/
			$data= $this->input->post('sortnum');
			foreach($data as $id=>$sort){
				$sql="UPDATE menuCategory SET mcViewsort='$sort' WHERE mcId='$id'";
				$this->db->query($sql);
			}
			redirect('menu/categoryindex');
		} 
		/* 
			CRUD Convention : 
			=================
			1. simpla/admin/index.php (Basic Container yang menampung view untuk CRUD)
			2. simpla/admin/settings/menu_view (View untuk menampilkan Read, Add, Update)
			3. Menu_model (Model CRUD)
			4. $data['action'] (Action untuk CRUD)
			5. $data['pagename']	(Nama Current Page)
			Menu_view -> kumpulkan semua data -> Masukkan ke index kembali.
		*/
		
		/* 
			Menampilkan menu
		*/
		function index(){
			$data['main_view']= 'simpla/admin/settings/menu_view';
			$data['menu_descripstion']="Menu ini digunakan untuk mengolah menu-menu yang ada pada halaman administrator.";
			$data['query']				  =$this->Menu_model->get_view();
			$data['action']			  =$this->action;
			$data['pagename']			  ="Menu";
			$this->load->view("simpla/admin/index",$data);
		}
		
		/* 
			Daftar kategori
		*/
		
		function categoryindex(){
			$this->action="categoryindex"; // --> For view
			$data['main_view']= 'simpla/admin/settings/menu_view';
			$data['menu_descripstion']="Menu ini digunakan untuk mengolah kategori menu yang ada pada halaman administrator.";
			$data['query']				  =$this->Menu_model->categoryindex();
			$data['action']			  =$this->action;
			$data['pagename']			  ="Kategori Menu";
			$this->load->view("simpla/admin/index",$data);
		}

		/* 
			Menambah kategori
		*/
		function addcategory(){
			$this->action="addcategory";
			$data['main_view']= 'simpla/admin/settings/menu_view';
			$data['menu_descripstion']="Menu ini digunakan untuk mengolah menu-menu yang ada pada halaman administrator.";
			$data['action']			  =$this->action;
			$data['proccess_url']		  ="addcategoryproses";
			$data['pagename']			  ="Tambah Kategori";
			$this->load->view("simpla/admin/index",$data);
		}

		function addcategoryproses(){
			//mcname mcdesc //	mcId	mcName	mcViewsort	mcDesc
			$this->action="addcategory";
			$data['main_view']= 'simpla/admin/settings/menu_view';
			$data['menu_descripstion']="Menu ini digunakan untuk mengolah menu-menu yang ada pada halaman administrator.";
			$data['action']			  =$this->action;
			$data['proccess_url']	  ="addcategoryproses";
			$data['pagename']			  ="Tambah Kategori";
			$this->load->view("simpla/admin/index",$data);

			//Validasi form
			$this->form_validation->set_rules('mcname', 'Category Name', 'required');

			if ($this->form_validation->run() == TRUE){
				// save data mcname mcdesc
				$category = array('mcId' => '',
								 'mcName' => $this->input->post('mcname'),
								 'mcViewsort' => $this->nextid('menuCategory','mcId'),
								 'mcDesc' => $this->input->post('mcdesc')
				);
				$this->Menu_model->addcategory($category);

				redirect('menu/categoryindex');
			}else{
 				$this->load->view($data['main_view']);
			}
		}

		function editcategory(){
			$id='';		
			if($this->uri->segment(3) === FALSE){
				$error=errorlist(1);
			}else{
				$id = $this->uri->segment(3);
			}

			$this->action					="editcategory";
			$data['main_view']			= 'simpla/admin/settings/menu_view';
			$data['menu_descripstion']	="Menu ini digunakan untuk mengolah menu-menu yang ada pada halaman administrator.";
			$data['action']				=$this->action;
			$data['query']					= $this->Menu_model->getcategorywithid($id);
			$data['proccess_url']		="editcategoryproccess/";
			$data['pagename']				="Edit Kategori";
			$this->load->view("simpla/admin/index",$data);
		}

		function editcategoryproccess(){
			//mcname mcdesc //	mcId	mcName	mcViewsort	mcDesc
			$id='';		
			if($this->uri->segment(3) === FALSE){
				$error=errorlist(1);
			}else{
				$id = $this->uri->segment(3);
			} 
			
			//Validasi form
			$this->form_validation->set_rules('mcname', 'Category Name', 'required');

			if ($this->form_validation->run() == TRUE){
				// save data mcname mcdesc
				$category = array(
										'mcName' => $this->input->post('mcname'),
										'mcDesc' => $this->input->post('mcdesc')
								);
				//$this->db->where('mcId', $id);
				$this->Menu_model->editcategoryproccess($id,$category);
				redirect('menu/categoryindex');
			}else{
 				$this->load->view('menu_view');
			}
		}

		function addmenu(){
			$this->action					="addmenu";
			$data['main_view']			='simpla/admin/settings/menu_view';
			$data['menu_descripstion']	="Menu ini digunakan untuk mengolah menu-menu yang ada pada halaman administrator.";
			$data['action']				=$this->action;
			$data['proccess_url']		="addmenuproses";
			$data['pagename']			  	="Tambah Menu";
			$this->load->view("simpla/admin/index",$data);
		}

		function addmenuproses(){
			//mcname mcdesc //	mcId	mcName	mcViewsort	mcDesc
			$this->action="addcategory";
			$data['main_view']= 'simpla/admin/settings/menu_view';
			$data['menu_descripstion']="Menu ini digunakan untuk mengolah menu-menu yang ada pada halaman administrator.";
			$data['action']			  =$this->action;
			$data['proccess_url']	  ="addmenuproses";
			$data['pagename']			  ="Tambah Menu";
			$this->load->view("simpla/admin/index",$data);

			//Validasi form
			$this->form_validation->set_rules('mcname', 'Category Name', 'required');

			if ($this->form_validation->run() == TRUE){
				// save data mcname mcdesc
				$category = array('mcId' => '',
								 'mcName' => $this->input->post('mcname'),
								 'mcViewsort' => $this->nextid('menuCategory','mcId'),
								 'mcDesc' => $this->input->post('mcdesc')
				);
				$this->Menu_model->add($category);
 
				header('addcategory');
			}else{	
 				$this->load->view('menu/index');
			}	
		}
		
		function editmenu(){
		
		
		}
		
		function update($nis){
			$data['title'] 			= $this->title;
			$data['h2_title'] 		= 'Siswa > Update';
			$data['main_view'] 		= 'siswa/siswa_form';
			$data['form_action']	= site_url('siswa/update_process');
			$data['link'] 			= array('link_back' => anchor('siswa','kembali', array('class' => 'back'))
											);

			// data kelas untuk dropdown menu
			$kelas = $this->Kelas_model->get_kelas()->result();
			foreach($kelas as $row)
			{
				$data['options_kelas'][$row->id_kelas] = $row->kelas;
			}
			
			// cari data dari database
			$siswa = $this->Siswa_model->get_siswa_by_id($nis);
			
			// buat session untuk menyimpan data primary key (nis)
			$this->session->set_userdata('nis', $siswa->nis);
			
			// Data untuk mengisi field2 form
			$data['default']['nis'] 		= $siswa->nis;
			$data['default']['nama'] 		= $siswa->nama;		
			$data['default']['id_kelas']	= $siswa->id_kelas;
					
			$this->load->view('template', $data);
		}
	
		function errorlist($msg){
			return "<li>".$msg[$msg]."</li>";
		}
	}
?>